<?php
/**
 * Vypis dynamickeho pohladu do HTML kodu
 * @author Tomas Srna
 * @package bakalarka
 */

class DynamicViewWriter implements Writer
{
	/**
	 * @var DynamicView $dv		Dynamicky pohlad
	 */
	protected $dv = NULL;
	
	/**
	 * @var Template $t 		Template
	 */
	protected $t = NULL;
	
	/**
	 * V kontruktore nastavime dynamicky pohlad a template
	 */
	public function __construct(DynamicView $dv, Template $t)
	{
		$this->dv = $dv;
		$this->t = $t;
		
		$this->t->addHook('headEnd', "<link rel=\"stylesheet\" ".
			"type=\"text/css\" href=\"".APPBASE."css/dynamic-view.css\" />");
		$this->t->addHook('headEnd', "<link rel=\"stylesheet\" ".
			"type=\"text/css\" href=\"".APPBASE."css/form.css\" />");
	}
	
	/**
	 * Vypis dynamickeho pohladu do HTML
	 */
	public function write()
	{
		$this->writeButtons();
		$this->writeCleaner();
		$this->writeContainer();
		$this->writeParams();
		$this->writeFunctions();
	}
	
	/**
	 * Vypis "cistica"
	 */
	public function writeCleaner()
	{
?>
<div class="cleaner">&nbsp;</div>
<?php
	}
	
	/**
	 * Vypis tlacitok
	 */
	public function writeButtons()
	{
?>
<div class="buttons">
	<div class="left">
<?php if($this->dv->buttonAdd): ?>
		<button onclick="act('create', false);">
			<img alt="" src="<?php echo APPBASE ?>img/add.png" />
			<?php echo L::selected()->add; ?>
		</button>
<?php endif; ?>
		<button onclick="reset();">
			<img alt="" src="<?php echo APPBASE ?>img/control_stop_blue.png" />
			<?php echo L::selected()->resetDynamicView; ?>
		</button>
<?php if(count($this->dv->filters) > 0): ?>
		<button onclick="filter(this);">
			<img alt="" src="<?php echo APPBASE ?>img/filter.png" />
			<?php echo L::selected()->enableFilters; ?>
		</button>
<?php endif; ?>
	</div>
	<div id="data-loading">
		<img alt ="" src="<?php echo APPBASE;?>img/ajax-loader.gif" /> 
		<?php echo L::selected()->loadingDynamicView; ?>
	</div>
	<div class="right">
		<span><?php echo L::selected()->page; ?>:</span>
		<button id="page-prev" onclick="prev();"><img alt="" src="<?php echo APPBASE;?>img/control_rewind_blue.png" /></button>
		<input type="text" id="page-number" value="0" onchange="gopage();" />
		<button id="page-next" onclick="next();"><img alt="" src="<?php echo APPBASE;?>img/control_fastforward_blue.png" /> </button>
	</div>
</div> <!-- end buttons -->
<?php
	}

	/**
	 * Vypise funkcie pre editaciu, zmenu hesla a zmazanie
	 */
	public function writeFunctions()
	{
		$arg2arr = "var arg = new Array(); for(var i = 0; i < arguments.length; ++i) {arg[i] = arguments[i];}";
?>
<script type="text/javascript">
<!--
	function act()
	{
		if(arguments.length < 2)
			return false;
			
		action = arguments[0];
		conf = arguments[1];
	
		var arg = new Array();
		for(var i = 2; i < arguments.length; ++i)
		{
			arg[i-2] = arguments[i];
		}
		
		if(!conf || confirm(L.confirmDelete))
			window.location.href='<?php 
			echo $this->dv->baseUrl; ?>/'+action+'?'
			+ pkeys(arg) + redirdata();
	}
	function pkeys(arg)
	{
		var ret = "";
		var pk = ["<?php echo implode("\",\"", $this->dv->primaryKeys); ?>"];
		for (var i = 0; i < arg.length; i++)
		{
			ret += pk[i] + "=" + arg[i];
			if(i+1 < arg.length)
				ret += "&";
		}
		return ret;
	}
	function redirdata()
	{
		var ret = "&redir=<?php 
		echo urlencode($this->dv->baseUrl); ?>";
		if(window.location.hash.length > 1)
			ret += "&hash="+escape(window.location.hash.substring(1));
		return ret;
	}
// -->
</script>

<?php
	}
	
	/**
	 * Vypise dynamicke parametre pohladu
	 */
	public function writeParams()
	{
?>
<script type="text/javascript">
<!--
var itemCount = <?php echo $this->dv->itemCount; ?>;
var itemsPerPage = <?php echo Config::$itemsPerPage; ?>;
var baseUrl = "<?php echo APPBASE;?>";
var dataUrl = "<?php echo $this->dv->baseUrl."/data";?>";
var enableFilters = <?php echo (count($this->dv->filters) ? "true" : "false"); ?>;
// -->
</script>

<?php
	}
	
	/**
	 * Vypis Ajaxoveho containtera
	 */
	public function writeContainer()
	{
?>
		<div id="ajax-container"></div>
<?php
	}
}
